package BasicEntity
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	public class Vehicle extends MovingEntity
	{
		public var TimeElapsed:Number;
		public var Frames:Vector.<BitmapData>;
		public var Frame:Sprite;
		public var ImgContainer:Bitmap;
		public var ImgIndex:int;
		
		public var friction:Number;
		
		public var Steer:SteeringBehaviors;
		public var Offset:Point;
		public var Scale:Number;
		
		public var radar:WanderRadar;
		
		public var target:Vector2D;
		public var targetB:Boolean;
		
		/**
		 * _pos:位置 _velicity:速度 _head:朝向 _mass:质量 _maxSpeed:最大速度 _maxTurnRate:最大转速 _maxForce:最大受力
		 * _offsetX:偏移 X _offsetY:偏移 Y  _scale:缩放
		 */
		public  function Vehicle(  _pos:Vector2D,
		                           _velocity:Vector2D,
								   _head:Vector2D,
								   _mass:Number,
								   _maxSpeed:Number,
								   _maxTurnRate:Number,
								   _maxForce:Number,
								   _offsetX:int,
								   _offsetY:int,
								   _scale:Number
								)
		{
			Steer = new SteeringBehaviors(this);
			super(_pos, _velocity, _head, _mass, _maxSpeed, _maxTurnRate, _maxForce);
			Frame = new Sprite();
			Offset = new Point(_offsetX, _offsetY);
			Scale = _scale;
			Frame.scaleX = Scale;
			Frame.scaleY = Scale;
			
			radar = new WanderRadar(0,-200,550);
			Frame.addChild(radar);	
			
			var shape:Shape = new Shape();
			shape.graphics.lineStyle(1, 0xff0000);
			shape.graphics.moveTo(-200, 0);
			shape.graphics.lineTo(200, 0);
			
			Frame.addChild(shape);
			var shape2:Shape = new Shape();
			shape2.graphics.lineStyle(1, 0xffff00);
			shape2.graphics.moveTo(0, -200);
			shape2.graphics.lineTo(0, 200);
			
			Frame.addChild(shape2);
			Init();
		}	
		
		
		public override function Update(_elapsedTime:Number):void 
		{
			super.Update(_elapsedTime);
			
			TimeElapsed = _elapsedTime;		
			
			var force:Vector2D = Steer.Calculate(); 			
			var acceleration:Vector2D = force.CrossProduct(1.0/Mass);
			var t1:Vector2D = acceleration.CrossProduct(_elapsedTime);
			
			Velocity =  Velocity.Addition(t1); //更新速度 
			
			if (Pos.X > 800 || Pos.Y > 600 || Pos.X < 0 || Pos.Y < 0)
			{
				Velocity = Velocity.CrossProduct( -1);
			}
			
			var t2:Vector2D = Velocity.CrossProduct(_elapsedTime);
			
			t2.Turncate(MaxSpeed);
			Pos = Pos.Addition(t2);	
			
			Frame.x = Pos.X;
			Frame.y = Pos.Y;
			
			Frame.rotation = Heading.Angle * 180 / Math.PI + 90 ;			
					
			if (Velocity.LengthSQ > 0.000001)
			{
				Heading = Velocity.NormalizeClone();
				Side = Heading.Perp;
			}
			if (int(_elapsedTime) % 40 == 0)
			{
			  GameWorld.GraphicsTool.beginFill(0x000000, 1);
			  GameWorld.GraphicsTool.drawCircle(Pos.X, Pos.Y, 1);
			}
		}
		
		public function UpdateFrame():void
		{
			ImgIndex++;
			if (ImgIndex > Frames.length - 1)
			   ImgIndex = 0;
			ImgContainer.bitmapData = Frames[ImgIndex];
		}
		
		private function Init():void
		{
			Frames = new Vector.<BitmapData>();
			for each(var item:BitmapData in GameWorld.ImgPool)
			{
				Frames.push(item);
			}
			ImgContainer = new Bitmap();
			
			ImgContainer.x =  -Offset.x ;
			ImgContainer.y =  -Offset.y ;
			
			Frame.addChild(ImgContainer);
			
			Frame.x = Pos.X;
			Frame.y = Pos.Y; 
					
				
			
			ImgContainer.addEventListener(Event.ENTER_FRAME, function(e:Event):void
			{
				UpdateFrame();
			});
		}
		
	}
}